package util

import (
	"fmt"
	"math"
	"strconv"
)

// CeilToInt64 向上取整 float64 -> int64
func CeilToInt64(x float64) int64 {
	return int64(math.Ceil(x))
}

// CeilToInt64 向上取整 float64 -> int
func CeilToInt(x float64) int {
	return int(math.Ceil(x))
}

// FloorToInt64 向下取整 float64 -> int64
func FloorToInt64(x float64) int64 {
	return int64(math.Floor(x))
}

// FloorToInt64 向下取整 float64 -> int
func FloorToInt(x float64) int {
	return int(math.Floor(x))
}

// RoundToInt64 四舍五入 float64 -> int64
func RoundToInt64(x float64) int64 {
	return int64(math.Floor(x + 0.5))
}

// RoundToInt32 四舍五入 float64 -> int32
func RoundToInt32(x float64) int32 {
	return int32(math.Floor(x + 0.5))
}

// AbsFromInt64ToUint64 int64 -> uint64
func AbsFromInt64ToUint64(x int64) uint64 {
	return uint64(math.Abs(float64(x)))
}

func Fix2Str(f float64) string {
	f1 := math.Trunc(f*1e2+0.5) * 1e-2
	value := strconv.FormatFloat(f1, 'f', 2, 64)
	return value
}

func Fix0Str(f float64) string {
	f1 := math.Trunc(f*1e0+0.5) * 1e-0
	value := strconv.FormatFloat(f1, 'f', 0, 64)
	return value
}

func Fix2(f float64) float64 {
	f1Str := Fix2Str(f)
	value, _ := strconv.ParseFloat(f1Str, 64)
	return value
}

func Fix0(f float64) float64 {
	f1Str := Fix0Str(f)
	value, _ := strconv.ParseFloat(f1Str, 64)
	return value
}

func RoundToPoint1(x float64) float64 {
	num1, _ := strconv.ParseFloat(fmt.Sprintf("%.1f", x), 64) // 保留1位小数
	return num1
}

func RoundToPoint2(x float64) float64 {
	num2, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", x), 64) // 保留2位小数
	return num2
}

func RoundToPoint3(x float64) float64 {
	num3, _ := strconv.ParseFloat(fmt.Sprintf("%.3f", x), 64) // 保留3位小数
	return num3
}

func MaxN [T Number] (x , y T) T {
	a := float64(x)
	b := float64(y)
	c := math.Max(a, b)
	if (a >= c) {
		return x
	}
	return y
}

func MinN [T Number] (x , y T) T {
	a := float64(x)
	b := float64(y)
	c := math.Min(a, b)
	if (a <= c) {
		return x
	}
	return y
}